package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.repository.ScheduleRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmentRepository departmentRepository;

    @Autowired
    ScheduleRepository scheduleRepository;

    @Autowired
    MongoTemplate mongoTemplate;

    /**
     * 上传科室的需求
     * 1. 签名校验(8个接口的校验规则是一样的)
     * 2. 判断科室是否存在,如果存在,执行更新;如果不存在,执行添加
     * @param map
     */
    @Override
    public void save(Map<String, Object> map) {


        //1.签名校验
        //2.判断科室是否存在
        String jsonString = JSON.toJSONString(map);  //将map转换成json
        Department department = JSON.parseObject(jsonString, Department.class); //将json转换成Department对象

        //判断科室是否存在,需要通过医院编号+科室编号
        Department departmentFromMongo = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        if(departmentFromMongo==null){
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            departmentRepository.insert(department);
        }else{
            department.setId(departmentFromMongo.getId());
            department.setCreateTime(departmentFromMongo.getCreateTime());
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        }


    }

    /**
     * 查询科室
     * @param map
     * @return
     */
    @Override
    public Page<Department> selectPage(Map<String, Object> map) {
/*
    1.签名校验(省略)
    2.将医院的参数获取到
 */
        String hoscode = String.valueOf(map.get("hoscode")); //根据医院编号进行条件查询
        int page = Integer.parseInt(String.valueOf(map.get("page")));
        int limit = Integer.parseInt(String.valueOf(map.get("limit")));

        //根据医院编号查询科室列表  hoscode=?
        Department department=new Department();
        department.setHoscode(hoscode);

        Example<Department> example=Example.of(department);
        //分页(注意: 0表示第一页)
        Pageable pageable= PageRequest.of(page-1,limit);

        Page<Department> all = departmentRepository.findAll(example, pageable);

        return all;
    }

    @Override
    public void remove(Map<String, Object> map) {
        //根据医院编号和科室编号删除某个科室
        String hoscode = String.valueOf(map.get("hoscode"));
        String depcode = String.valueOf(map.get("depcode"));

        Department departmentFromMongo = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if(departmentFromMongo!=null){
            String id = departmentFromMongo.getId(); //科室的主键
            departmentRepository.deleteById(id);
        }

        //departmentRepository.removeByHoscodeAndDepcode(hoscode,depcode);

        //删除科室时,同时删除科室下的排班(根据hoscode+depcode删除所有的排班)
        //scheduleRepository.deleteByHoscodeAndDepcode(hoscode,depcode);

        Query query=new Query();
        query.addCriteria(Criteria.where("hoscode").is(hoscode).and("depcode").is(depcode));
        mongoTemplate.remove(query, Schedule.class);
    }
}
